/**
 ******************************************************************************
 * @file    Project/STM32F4xx_StdPeriph_Templates/main.c 
 * @author  MCD Application Team
 * @version V1.6.1
 * @date    21-October-2015
 * @brief   Main program body
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
 *
 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *        http://www.st.com/software_license_agreement_liberty_v2
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "mod_filter.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define LEVEL_MASK     0x01
#define EDGE_MASK      0x02
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/


uint8_t mod_bitfilter_state(bitfilter_t *filter)
{
    return ( (*(uint8_t*)filter) & (EDGE_MASK | LEVEL_MASK));
}

uint8_t mod_bitfilter_edge(bitfilter_t *filter)
{
    return (filter->edge);
}

uint8_t mod_bitfilter_level(bitfilter_t *filter)
{
    return (filter->level);
}

void mod_bitfilter_init(bitfilter_t *filter ,uint8_t high_cmp, uint8_t low_cmp)
{
    (*(uint8_t*)filter) = 0;         //clr Leven Edge oldLeven oldBIT nc
    filter->high_cmp = high_cmp;
    filter->low_cmp = low_cmp;   
}

void mod_bitfilter_poll(uint8_t bit_val, bitfilter_t *filter)
{
    if( filter->old_bit == bit_val )
    {
        if( filter->cnt != 0 )
            filter->cnt--;
        else
            filter->level = bit_val;
    }
    else
    {
        if( bit_val )
            filter->cnt = filter->high_cmp;
        else
            filter->cnt = filter->low_cmp;
    }

    filter->edge = (filter->level ^ filter->old_level) ? 1 : 0;

    filter->old_bit = bit_val;
    filter->old_level = filter->level;

}


/**
 * @}
 */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
